package com.bw.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;



import com.bw.domain.User;
import com.bw.service.UserService;
import com.bw.utils.R;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.annotations.ApiOperation;
import lombok.experimental.PackagePrivate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author bobo棒
 * @date 2024年06月27日 11:29
 */

@ApiOperation("用户模块")
@RestController
@RequestMapping("auth")
public class LoginController {
    @Autowired
    UserService userService;


    @ApiOperation("登录")
    @PostMapping("login")
    public R login(@RequestBody User user ){
        //构建查询条件-根据用户名精确查询
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername());
        User dbUser = userService.getOne(wrapper);

        if(dbUser == null)  {
            return  R.error("无此账户");
        }
        if(!dbUser.getPassword().equals(user.getPassword())){
            return  R.error("密码错误");
        }
        //登录成功--返回token
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",dbUser.getId());

        String token = Jwts.builder().setClaims(map)
                .signWith(SignatureAlgorithm.HS256, "BW".getBytes()).compact();
        return R.success(token);
    }

    }

